iT邦幫忙

0

30天 Leetcode挑戰_Day 1

  • 分享至 

  • xImage
  •  

本身不是資訊相關科系的學生,因此想要藉著鐵人賽的機會提升自己的程式能力,並複習資料結構中所學的東西。

**目前規劃**

預計使用比較熟悉的C/C++來練習
Day1~Day10 Easy題重新回憶一下寫程式的感覺
Day11~Day30 Medium題提升自己的實力

一直到現在才知道vector這個語法~~
#題目敘述:

  1. Two Sum
    Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int n=nums.size();
        int ans;
        vector<int> output(2);
        for(int i=0;i<n;i++){
            ans=target-nums[i];
            for(int j=i+1;j<n;j++){
                if(ans==nums[j]){
                     output ={i,j};

                }

            }
        }
        return output;
    }
};

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言